package at.fhooe.mcm12.java.sensors;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.Random;
import at.fhooe.mcm12.java.enums.OutputFormat;
import at.fhooe.mcm12.java.sensors.data.Humidity;
import at.fhooe.mcm12.java.sensors.data.SensorValue;
import at.fhooe.mcm12.java.serializer.HumiditySerializer;

public class HumiditySensor extends PollSensor {

	private Random m_humidityGenerator;
	
	public HumiditySensor()throws RemoteException
	{
		super();
		Date dt = new Date();
		
		m_dataType = new Humidity(0,dt.getTime());
		m_humidityGenerator = new Random();
		m_serializer = new HumiditySerializer();
		m_isSubscribable = true;
		
		m_description = "Provides information about the current humidity in %.";
		Thread t = new Thread(this);
		t.start();
	}

	@Override
	public void serialize(OutputFormat _format, SensorValue _value) {
		m_serializer.serialize(_format, _value);
	}

	@Override
	public SensorValue getSensorValue() {
		serialize(OutputFormat.XML, m_dataType);
		serialize(OutputFormat.CSV, m_dataType);
		serialize(OutputFormat.Text, m_dataType);
		
		return m_dataType;
	}

	@Override
	public void refreshSensorValue() {
		m_dataType.setTimestamp(new Date().getTime());
		((Humidity)m_dataType).setHumidity(20 + (m_humidityGenerator.nextGaussian() * 5));
	}
}
